/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    bool dfs(TreeNode* root)
    {
        if (root->val == 0 && root->left == nullptr && root->right == nullptr) return false;

        bool Left = root->left ? dfs(root->left) : false;
        bool Right = root->right ? dfs(root->right) : false;
        if (!Left)
        {
            root->left = nullptr;
        }
        if (!Right)
        {
            root->right = nullptr;
        }
        return root->val == 0 && !Right && !Left ? false : true;
    }

    TreeNode* pruneTree(TreeNode* root)
    {

        return dfs(root) == true ? root : nullptr;

    }
};